package com.yunchao.bank.business.pesentation.screen.info

import android.os.Bundle
import android.view.View
import by.kirich1409.viewbindingdelegate.viewBinding
import com.yunchao.feature.base.presentation.activity.BaseFragment
import com.yunchao.bank.R
import com.yunchao.bank.business.domain.model.BusinessInfoModel
import com.yunchao.bank.databinding.FragmentBusinessInfoBinding
import com.yunchao.bank.main.data.model.MetadataUserModel
import com.yunchao.bank.main.presentation.screen.MainViewModel
import com.yunchao.feature.base.presentation.activity.BaseActivity.Companion.mainActivity
import com.yunchao.feature.base.presentation.ext.setDebouncedClickListener
import com.yunchao.feature.base.presentation.ext.toast
import org.koin.androidx.navigation.koinNavGraphViewModel
import org.koin.androidx.viewmodel.ext.android.activityViewModel
import kotlin.getValue

internal class BusinessInfoFragment : BaseFragment(R.layout.fragment_business_info) {

    private val viewModel: BusinessInfoViewModel by koinNavGraphViewModel(R.id.peopleBusinessNavGraph)
    private val binding: FragmentBusinessInfoBinding by viewBinding<FragmentBusinessInfoBinding>()
    private val activityViewModel: MainViewModel by activityViewModel<MainViewModel>()

    private var businessInfoModel: BusinessInfoModel? = null
    private var operatorUserInfo: MetadataUserModel? = null
    private var auditorUserInfo: MetadataUserModel? = null

    override fun initView(view: View, savedInstanceState: Bundle?) {
        binding.taskTypeTv.setDebouncedClickListener {
            businessInfoModel?.let {
                viewModel.navigateToDetails(it)
            } ?: toast(com.yunchao.feature.base.R.string.暂无待处理的任务)
        }
    }


     fun handleMetadataUserInfo(metadataUserModel: MetadataUserModel?) {
         metadataUserModel?.apply {
                when (3) {
                    1 -> handleSingleUserInfo(this)
                    2 -> handleDoubleUserInfo(this)
                    3 -> handleTripleUserInfo(this)
                }
            } ?: run {
                activityViewModel.sendCardCommand(
                    isStopSend = binding.taskOperatorTv.text.isNotBlank() && binding.taskAuthorTv.text.isNotBlank() && binding.taskFollowTv.text.isNotBlank()
                )
            }
    }

    fun handleBusinessInfo(model: BusinessInfoModel) {
        businessInfoModel = model
        binding.taskTypeTv.text = model.businessName
        mainActivity.supportActionBar?.title = getString(
            com.yunchao.feature.base.R.string.有任务待处理请授权,
            model.taskId
        )

    }

    private fun handleSingleUserInfo(metadataUserModel: MetadataUserModel) {
        operatorUserInfo = metadataUserModel
        auditorUserInfo = metadataUserModel
        binding.taskOperatorTv.text =
            getString(com.yunchao.feature.base.R.string.操作人, metadataUserModel.name)
    }

    private fun handleDoubleUserInfo(metadataUserModel: MetadataUserModel) {
        if (binding.taskOperatorTv.text.isBlank()) {
            operatorUserInfo = metadataUserModel
            binding.taskOperatorTv.text =
                getString(com.yunchao.feature.base.R.string.操作人, metadataUserModel.name)
        } else if (binding.taskOperatorTv.text != (metadataUserModel.name)) {
            auditorUserInfo = metadataUserModel
            binding.taskAuthorTv.text =
                getString(com.yunchao.feature.base.R.string.授权人STR, metadataUserModel.name)
        }
    }

    private fun handleTripleUserInfo(metadataUserModel: MetadataUserModel) {
        // 检查是否已存在相同名称的用户
        if (isUserAlreadyAssigned(metadataUserModel.name)) {
            return // 如果已存在，则不做任何操作
        }

        // 按优先级顺序尝试填充空字段
        when {
            binding.taskOperatorTv.text.isBlank() -> {
                operatorUserInfo = metadataUserModel
                binding.taskOperatorTv.text =
                    getString(com.yunchao.feature.base.R.string.操作人, metadataUserModel.name)
            }

            binding.taskAuthorTv.text.isBlank() -> {
                auditorUserInfo = metadataUserModel
                binding.taskAuthorTv.text =
                    getString(com.yunchao.feature.base.R.string.授权人STR, metadataUserModel.name)
            }

            binding.taskFollowTv.text.isBlank() -> {
                binding.taskFollowTv.text =
                    getString(com.yunchao.feature.base.R.string.随行人, metadataUserModel.name)
            }
        }
    }

    // 检查用户是否已被分配到任意字段
    private fun isUserAlreadyAssigned(name: String): Boolean {
        return binding.taskOperatorTv.text == name ||
                binding.taskAuthorTv.text == name ||
                binding.taskFollowTv.text == name
    }


}